﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.Catalog 
{
    public partial class FrmTKB : PRE.Catalog.FrmBase
    {
        DAL.EntitesDAL.TKBDAL _dal = new DAL.EntitesDAL.TKBDAL();
        DAL.EntitesDAL.CahocDAL _dal1 = new DAL.EntitesDAL.CahocDAL();
        DAL.EntitesDAL.PhonghocDAL _dal2 = new DAL.EntitesDAL.PhonghocDAL();
        DAL.EntitesDAL.LophocDAL _dal3 = new DAL.EntitesDAL.LophocDAL();
        DAL.EntitesDAL.KhoahocDAL _dal5 = new DAL.EntitesDAL.KhoahocDAL();
        BLL.EntitiesBLL.TKBBLL _bll = new BLL.EntitiesBLL.TKBBLL();

        public FrmTKB()
        {
            InitializeComponent();
        }


        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            ReadOnlyControl(false);
            ChangeStatus(false);
            ResetText();
            dteNgayhoc.Focus();
            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var id1 = grvMain.GetFocusedRowCellValue("Ngayhoc") + "";
            var id2 = grvMain.GetFocusedRowCellValue("MacaID") + "";
            var id3 = grvMain.GetFocusedRowCellValue("MaphongID") + "";
            var id4 = grvMain.GetFocusedRowCellValue("MalopID") + "";

            DialogResult result;
            var ok = _dal.Delete(id1, id2, id3, id4);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus(false);
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            ResetText();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }
            
            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            if (IsAdd)
            {
                DateTime _ngay = Convert.ToDateTime(dteNgayhoc.EditValue);
                string _cahoc = cboCahoc.EditValue.ToString();
                string _lophoc = cboLophoc.EditValue.ToString();
                string _phong = cboPhonghoc.EditValue.ToString();

                if (_bll.Check_TKB(_ngay, _cahoc, _lophoc))
                {
                    if (_bll.Check_TKB_phong(_ngay, _cahoc, _phong))
                    {
                        var o = new DAL.Entities.TKB()
                        {
                            Ngayhoc = Convert.ToDateTime(dteNgayhoc.EditValue),
                            MacaID = cboCahoc.EditValue + "",
                            MaphongID = cboPhonghoc.EditValue + "",
                            MalopID = cboLophoc.EditValue + ""
                        };

                        var oki = _dal.Insert(o);
                        if (oki)
                        {
                            //MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            PerformRefresh();
                            ChangeStatus(false);
                            PerformAdd();
                        }
                        else
                        {
                            MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            ChangeStatus(false);
                            ReadOnlyControl(false);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Lỗi! Phòng này đã có thời khóa biểu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        ChangeStatus(false);
                        ReadOnlyControl(false);
                    }
                }
                else
                {
                    MessageBox.Show("Lỗi! Lớp này đã có thời khóa biểu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            if (IsEdit)
            {
                DateTime id1 = Convert.ToDateTime(grvMain.GetFocusedRowCellValue("Ngayhoc"));
                var id2 = grvMain.GetFocusedRowCellValue("MacaID") + "";
                var id3 = grvMain.GetFocusedRowCellValue("MaphongID") + "";
                var id4 = grvMain.GetFocusedRowCellValue("MalopID") + "";

                DateTime a = Convert.ToDateTime(dteNgayhoc.EditValue);
                string b = cboCahoc.EditValue + "";
                string c = cboPhonghoc.EditValue + "";
                string d = cboLophoc.EditValue + "";

                var oki = _dal.Update(id1,id2,id3,id4,a,b,c,d);
                if (oki)
                {
                    // MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            dteNgayhoc.EditValue = DateTime.Now;
            cboCahoc.ItemIndex = -1;
            cboPhonghoc.ItemIndex = -1;
            cboLophoc.ItemIndex = -1;
            
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            dteNgayhoc.DataBindings.Clear();
            cboCahoc.DataBindings.Clear();
            cboPhonghoc.DataBindings.Clear();
            cboLophoc.DataBindings.Clear();
            
            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {

            dteNgayhoc.DataBindings.Add("EditValue", _dtb, ".Ngayhoc");
            cboCahoc.DataBindings.Add("EditValue", _dtb, ".MacaID");
            cboPhonghoc.DataBindings.Add("EditValue", _dtb, ".MaphongID");
            cboLophoc.DataBindings.Add("EditValue", _dtb, ".MalopID");
            
            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            dteNgayhoc.Properties.ReadOnly = isReadOnly;
            cboCahoc.Properties.ReadOnly = isReadOnly;
            cboPhonghoc.Properties.ReadOnly = isReadOnly;
            cboLophoc.Properties.ReadOnly = isReadOnly;
            
            grcMain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ChangeStatus(false);
            ReadOnlyControl(false);
            ResetText();

            grcMain.Enabled = true;

            base.PerformSearch();
        }

        /// <summary>
        /// Execute Search data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGoSearch_Click(object sender, EventArgs e)
        {
        //    var o = new DAL.Entities.Daylop()
        //    {
        //        MaGVID = Convert.ToString(cboGiaoVien.EditValue),
        //        MalopID = Convert.ToString(cboGiaovien.EditValue),
        //        Dayphan = Convert.ToString(ckcboDayphan.EditValue)
        //    };

        //    grcMain.DataSource = _dal.Search(o);

        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dtb = _dal.Select();

            DataTable _dtb1 = _dal1.Select();
            cboCahoc.Properties.DataSource = _dtb1;

            DataTable _dtb2 = _dal2.Select();
            cboPhonghoc.Properties.DataSource = _dtb2;

            DataTable tmp = _dal5.Select_Current(DateTime.Now);
            DataTable _dtb3 = _dal3.Select(tmp.Rows[0]["Makhoa"] + "");
            cboLophoc.Properties.DataSource = _dtb3;

            if (_dtb != null)
            {
                grcMain.DataSource = _dtb;
                //gridColumn2.BestFit(); // fit column                
            }

            base.LoadData();
        }
        #endregion

        private void FrmTKB_Load(object sender, EventArgs e)
        {
            PerformRefresh();
        }

        private void cboLophoc_EditValueChanged(object sender, EventArgs e)
        {

        }
    }
}
